var init;
var second = 0;
var api = require("../../utils/api.js");
Page({
  data: {
    // 是否已开始录制
    isStart: false,
    statusText: "开始录制",
    statusIcon: "../../image/video.png",
    time: "00:00:00",
    // 是否显示重新录制
    isAgain: false,
    // 是否显示重新录制
    isProgress: false,
    uploadText: "上传中（0%）",
    src: "",
    videoSrc: "",
    taskId: ""
  },

  onLoad: function (options) {
    this.ctx = wx.createCameraContext();
    this.setData({
      taskId: options.taskId
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  start() {
    var that = this;
    second = 0;
    this.setData({
      time: '00:00:00'
    });
    init = setInterval(function () {
      second++;
      var second1 = second < 10 ? "0" + second : second;
      that.setData({
        time: "00:00:" + second1
      })
      if (second == 15) {
        that.startRecord();
      }
    }, 1000);
  },
  stop() {
    clearInterval(init);
  },
  startRecord() {
    if (!this.data.isStart) {
      this.ctx.startRecord({
        success: () => {
          console.log('startRecord');
          this.setData({
            isStart: true,
            statusText: "结束录制",
            statusIcon: "../../image/video1.png"
          });
          this.start();
        }
      });
    } else {
      this.ctx.stopRecord({
        success: (res) => {
          this.stop();
          console.log('stopRecord ' + res.tempVideoPath);
          this.setData({
            isAgain: true,
            src: res.tempThumbPath,
            videoSrc: res.tempVideoPath
          });
        }
      })
    }
  },
  again() {
    this.setData({
      isAgain: false,
      isStart: false,
      statusText: "开始录制",
      statusIcon: "../../image/video.png",
    })
  },
  submit() {
    api.client.getUserId(function (res) {}, this.data.taskId, this.data.videoSrc, this);
  }
})